<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Flag extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('flag_model');
		/*
		$this->load->model('contact_model');	
		$this->load->model('company_model');	
		$this->load->model('titles_model');	
		*/ 
	}
	
	public function index()
	{
		$data['msg'] 	= NULL;
		$this->load->view('flag/main', $data);
	}
	
	function get_info($id=NULL)
	{
		if(!$id)
		{
			$list = $this->flag_model->where('deleted',0)->find_all();
			$contact_group = array();
			//$data['num_row'] = $list->num_rows();

			foreach($list as $row){
				$contact_group[mb_substr($row->name_ar,0,1,'utf-8')][] = array("id" => $row->id, "name" => $row->name_ar);
			}
			
			$data['list']=$contact_group;
		}
		
		if($id)
		{
			//sleep(1);
			$data['list']  = $this->flag_model->find($id);
			
		}

		die(json_encode($data['list']));
		
	}
			
	function form($id=NULL)
	{
		//sleep(1);
		$data['flag']='';
		$data['flags'] = $this->flag_model->where('deleted',0)->find_all();
		
		if(!empty($id))
			$data['flag'] = $this->flag_model->find($id);
			
		$this->load->view('flag/form', $data);
	}
	
	function delete($id=NULL)
	{
		if (!empty($id))
		{	
			//check if the flag has no attached contact in contacts table
			
				if ($this->flag_model->delete($id))
				{
					//success
						$data['msg']	= ' Successfully Deleted..';
						$data['error']	= false;
						$this->log('Flag', 'Delete record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
					}
				else{
					//failed to delete
					$data['error']	= true;
					$data['msg']	= ' Failed to Delete..';
				}
		die(json_encode($data));
		}
		
	}
	
	function company($com_id)
	{
		if(!$com_id)
			die('Error');
		
		$data['id'] 			= $com_id;
		$data['form_action'] 	= 'flag/save_flag_company/';
		$data['flags'] 			= $this->flag_model->where(array('deleted'=>0,'parent'=>0))->find_all();
		$data['allflags'] 		= $this->flag_model->where(array('deleted'=>0))->find_all();
		$data['selected_flags']	= $this->flag_model->get_flag_company($com_id);
		
		$this->load->view('flag/form_all_flags', $data);
	}
	
	function role($role_id)
	{
		if(!$role_id)
			die('Error');
		
		$data['id'] = $role_id;
		$data['form_action'] = 'flag/save_flag_role/';
		$data['flags'] = $this->flag_model->where(array('deleted'=>0,'parent'=>0))->find_all();
		$data['allflags'] 		= $this->flag_model->where(array('deleted'=>0))->find_all();
		$data['selected_flags']	= $this->flag_model->get_flag_role($role_id);
		
		$this->load->view('flag/form_all_flags', $data);
	}
	
	function contact($contact_id)
	{
		if(!$contact_id)
			die('Error');
		
		$data['id'] 			= $contact_id;
		$data['form_action'] 	= 'flag/save_flag_contact/';
		$data['flags'] 			= $this->flag_model->where(array('deleted'=>0,'parent'=>0))->find_all();
		$data['allflags'] 		= $this->flag_model->where(array('deleted'=>0))->find_all();
		$data['selected_flags']	= $this->flag_model->get_flag_contact($contact_id);

		$this->load->view('flag/form_all_flags', $data);
	}
	
	
	function save_flag_contact()
	{
		$form['contact_id'] = $this->input->post('id');
		$form['flag_id'] 	= $this->input->post('checkbox');
		
		$result = $this->flag_model->flag_contact($form);
		$this->log('Flag', 'Add/Remove Flag to Contact with ID: '.$form['contact_id'].' : '.$this->session->userdata['ip_address']);
		die($result);
	}
	
	function save_flag_company()
	{
		$form['company_id'] = $this->input->post('id');
		$form['flag_id'] 	= $this->input->post('checkbox');
		
		$result = $this->flag_model->flag_company($form);
		$this->log('Flag', 'Add/Remove Flag to Company with ID: '.$form['company_id'].' : '.$this->session->userdata['ip_address']);
		die($result);
	}
		
	
	function save_flag_role()
	{
		$form['role_id'] = $this->input->post('id');
		$form['flag_id'] 	= $this->input->post('checkbox');
		
		$result = $this->flag_model->flag_role($form);
		$this->log('Flag', 'Add/Remove Flag to Role with ID: '.$form['role_id'].' : '.$this->session->userdata['ip_address']);
		die($result);
	}
		
		
	function create($form)
	{
		if($inserted_id = $this->flag_model->insert($form))
		{
			$data['error']= false;
			$data['id'] = $inserted_id;
			$data['msg']  = '<span style="color:green">New Flag :<b>"'.$form['name_en'].'"</b> Has Been Added Successfully<br>Thanks You !</span>';
		}
		else{
			$data['error']= true;
			$data['msg']  = 'Data Base Error <br> ERROR: flg/ins/100';
		}
			
		return $data;
	}
			
	function edit($id,$form)
	{
		if($this->flag_model->update($id, $form)){
			$data['error']= false;
			$data['msg']= $form['name_en'].' Successfully Updated..';
		}
		else{
			$data['error']= true;
			$data['msg']  = 'Data Base Error <br> ERROR: flg/upd/101';
		}
			
		return $data;
	}	
			
	public function save()
	{
		$this->form_validation->set_message('is_unique', "The<b> %s </b>already Exist!");
		$this->form_validation->set_message('is_unique2', "The<b> %s </b>already Exist!");
		
		//check if it new record or update to set the validation rules
		$id = $this->input->post('id');
		//insert
		if(empty($id) && $id=='')
		{
		$this->form_validation->set_rules('name_ar'	, 'Arabic Flag Name' , 'trim|required|min_length[3]|max_length[255]');
		$this->form_validation->set_rules('name_en'	, 'English Flag Name', 'trim|required|min_length[2]|max_length[255]');
		}
		
		//update
		if($id && !empty($id))
		{
		$this->form_validation->set_rules('name_ar'	, 'Arabic Flag Name', 'trim|required|min_length[3]|max_length[255]');
		$this->form_validation->set_rules('name_en'	, 'English Flag Name','trim|required|min_length[2]|max_length[255]');
		}
				
		$this->form_validation->set_rules('parent'	, 'Parent Flag'		, 'trim|required');		
		if ($this->form_validation->run() == FALSE)
		{
			$data['error']= true;
			$data['msg']  = $this->form_validation->getErrorsArray();			
			die(json_encode($data));
			//die(json_encode($errors));
		}
		else
		{
			$form['name_ar'] 	= ucwords(strtolower($this->input->post('name_ar')));
			$form['name_en'] 	= ucwords(strtolower($this->input->post('name_en')));
			$form['parent'] 	= $this->input->post('parent');
			//NEW/Insert
			if($this->input->post('id')=='')
			{
				$data = $this->create($form);
				$this->log('Flag', 'Create Record with ID: '.$data['id'].' : '.$this->session->userdata['ip_address']);
			}
			//Edit/Update
			if($this->input->post('id'))
			{
				$id = $this->input->post('id');
				$data = $this->edit($id, $form);
				$this->log('Flag', 'Updated Record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
			}
			die(json_encode($data));
		}
	}
	
	function get_child_tags($parent_id)
	{
		//echo $parent_id;
		//if($parent_id)
		//{
			//sleep(1);
			$data['list']  = $this->flag_model->where('parent',$parent_id)->find_all();
			//print_r($this->db->last_query());
			
		//}

		die(json_encode($data['list']));
	}

	function build_ul_view()
	{
		$data['allflags'] 		= $this->flag_model->where(array('deleted'=>0))->find_all();
		$this->load->view('flag/ul_all_flags', $data);
		
		$this->log('Flag', 'Browse all flags as a Tree View from: '.$this->session->userdata['ip_address']);
	}
}
